package com.ma.controller;

import com.ma.entity.*;
import com.ma.server.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Size;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

/**
 * @ProjectName: project
 * @Package: com.ma.controller
 * @ClassName: teacherCon
 * @Author: YHHHH
 * @Description:   老师的控制器
 * @Date: 2021/3/29 16:28
 * @Version: 1.0
 */
@RestController
@CrossOrigin
public class teacherCon {

    @Autowired
    private com.ma.server.teamServer teamServer;
    @Autowired
    private LoginServer loginServer;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StudentServer studentServer;
    @Autowired
    private MessageServe messageServe;
    @Autowired
    private TeacherServer teacherServer;

    /*
    老师登陆
     */
    @PostMapping("/login/TeacherLogin")
    public TeacherVO TeacherLogin(@RequestBody TeacherVO teacherVO){
        TeacherVO loginTeacher = teacherServer.loginTeacher(teacherVO.getNumber());
        System.out.println(loginTeacher);
        if (loginTeacher!=null){
            return loginTeacher;
        }else {
            return null;
        }
    }

    /*
    老师登陆后返回老师的具体信息
     */
    @GetMapping("/teacher/TeacherData")
    public Teacher TeacherData(@RequestParam("id") String id){
        int teacherId = Integer.parseInt(id);
        Teacher teacher = teacherServer.teacherData(teacherId);
        return  teacher;
    }

   /*
   打分
    */
   @PostMapping("/tea/makePoint")
   @Transactional
    public String makePoint(@RequestBody Map<Object,Object> map){
       Integer teamPoint = (Integer) map.get("teamPoint");
       Integer teamId = (Integer) map.get("teamId");
       List<Integer> matePoint = (List<Integer>) map.get("matePoint");  //所有组员的分数
       //拿到所有组员
       List<String> allTeammateNum = studentServer.findAllTeammateNum(teamId);
       System.out.println(allTeammateNum);
       //给所有组员打分
       for (int i = 0; i < allTeammateNum.size(); i++) {
           String number = allTeammateNum.get(i);
           Integer score = matePoint.get(i);
           Integer integer = studentServer.updateScore(score, number);
       }
       //给小组评分
       Integer integer = teamServer.updateScore(teamPoint, teamId);
       //将redis的数据更新

       redisTemplate.delete(allTeammateNum);
       return "success";
   }
}

